//
//  WYMainViewController.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/9.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit
import SVProgressHUD

class WYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        let wyTabbar = WYTabBar()
        
        wyTabbar.composebuttonClosure = { [weak self] in
        
            print("我是代理调过来的")
            
            if !WYUserAccountViewModel.sharedUserAccount.isLogin {
            
                SVProgressHUD.showErrorWithStatus("请先登录")
                return
            }
            //代码执行到此 一定有值
            if let weakSelf = self {
            
                 let composeView = WYCompose()
                composeView.show(weakSelf)
            }
           
//            let window = UIApplication.sharedApplication().windows.last!
//            
//            window.addSubview(composeView)
        }
        
        setValue(wyTabbar, forKey: "tabBar")
        
        addChildViewController(WYHomeTableViewController(), title: "主页", imageName: "tabbar_home")
        addChildViewController(WYMessageTableViewController(), title: "信息", imageName: "tabbar_message_center")
        addChildViewController(WYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(WYProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
    }
    
    //重载的方法
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        
        //设置自定义 tabbarItem
        childController.tabBarItem = WYTabBarItem()
        
        childController.title = title
        //tabarItem 内容
//        childController.tabBarItem.title = title
        //设置图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //设置选择图片
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        //设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)
        //设置字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        //设置 按照照片本身的状态加载
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //添加自控制器 
        let nav = UINavigationController(rootViewController: childController)
       
        addChildViewController(nav)
        
    }

 

}
